package map;

import javax.swing.*;
import java.awt.event.*;
import java.awt.*;

// Frame for holding the map editor
public class Launcher {

	boolean pselect, wselect, oselect;
	JFrame frame;
	DrawPanel dp;
	JButton player, wall, opponent, reset, save;
	String mapName;
    
 //   public static void main(String[] args) {
 //       Launch l = new Launch();
//		l.go();
//	}
    
    public Launcher(String s) { this.mapName = s; }
	
	public void go() {
		frame = new JFrame();
        dp = new DrawPanel(mapName);
        JPanel south = new JPanel();
        player = new JButton("Player");
        wall = new JButton("Wall");
        opponent = new JButton("Opponent");
		reset = new JButton("Reset");
		save = new JButton("Save");
        south.add(player);
        south.add(wall);
        south.add(opponent);
		south.add(reset);
		south.add(save);
        player.addActionListener(new playerListener());
        wall.addActionListener(new wallListener());
        opponent.addActionListener(new opponentListener());
		reset.addActionListener(new resetListener());
		save.addActionListener(new saveListener());
        frame.getContentPane().add(BorderLayout.CENTER, dp);
        frame.getContentPane().add(BorderLayout.SOUTH, south);
        if(dp.getPlayerXPos() >= 0 && dp.getPlayerYPos() >= 0)
            player.setEnabled(false);
		JPanel east = new JPanel();
		JLabel legend = new JLabel("Legend Color:Piece");
		JLabel p = new JLabel ("Red : Player");
		JLabel o = new JLabel ("Orange : Opponent");
		JLabel w = new JLabel ("Black : Wall");
		JLabel m = new JLabel ("White : Floor");
		east.setLayout(new BoxLayout(east, BoxLayout.Y_AXIS));
		east.add(legend);
		east.add(p);
		east.add(o);
		east.add(w);
		east.add(m);
		frame.getContentPane().add(BorderLayout.EAST, east);
        frame.setSize(540, 400);
        frame.setVisible(true);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    }

    public boolean getPSelect() { return pselect; }
    public boolean getWSelect() { return wselect; }
    public boolean getOSelect() { return oselect; }

    class playerListener implements ActionListener {
        public void actionPerformed(ActionEvent e) {
     //   	if(dp.getPlayerXPos() >= 0 && dp.getPlayerYPos() >= 0)
     //   		player.setEnabled(false);
            pselect = true;
			System.out.println("Launch pselect: " + pselect);
			dp.setPSelect(pselect);
			pselect = false;
		//	dp.setPSelect(pselect);
			player.setEnabled(false);
        }
    }

    class wallListener implements ActionListener {
        public void actionPerformed(ActionEvent e) {
            wselect = true;
			System.out.println("Launch wselect: " + wselect);
			dp.setWSelect(wselect);
			wselect = false;
		//	dp.setWSelect(wselect);
        }
    }

    class opponentListener implements ActionListener {
        public void actionPerformed(ActionEvent e) {
            oselect = true;
			System.out.println("Launch oselect: " + oselect);
			dp.setOSelect(oselect);
			oselect = false;
		//	dp.setOSelect(oselect);
        } 
    }
	
	class resetListener implements ActionListener {
		public void actionPerformed(ActionEvent e) {
			player.setEnabled(true);
			dp.reset();
		}
	}
	
	class saveListener implements ActionListener {
		public void actionPerformed(ActionEvent e) {
			dp.saveMap();
			frame.dispose();
			int mc = JOptionPane.INFORMATION_MESSAGE;
			String message = "File saved: testmap.txt";
			JOptionPane.showMessageDialog(null, message, "Alert", mc);

		}
	}
}
